import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yp/config/config.dart';
import 'package:yp/router.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'logic.dart';
import 'state.dart';
import 'widgets/item_widget1.dart';
import 'widgets/item_widget2.dart';

class OrderListPage extends StatefulWidget {
  OrderListPage({Key key}) : super(key: key);

  @override
  _OrderListPageState createState() {
    return _OrderListPageState();
  }
}

class _OrderListPageState extends State<OrderListPage> with SingleTickerProviderStateMixin {

  final OrderListLogic logic = Get.put(OrderListLogic());
  final OrderListState state = Get.find<OrderListLogic>().state;
@override
void initState() {

   state.controller = TextEditingController();
  state.controller1 = RefreshController();
    // TODO: implement initState
    super.initState();
  }

   @override
  void dispose() {
  print('销毁');
  state.streamSubscription.cancel();

    state.streamSubscription.pause();

    Get.delete<OrderListLogic>();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {

    return WillPopScope(
      // onWillPop: () {
      //   Get.offAllNamed(RouteConfig.main);
      //   return;
      // },
      child: GetBuilder<OrderListLogic>(
        builder: (logic) {
          return Scaffold(
            resizeToAvoidBottomInset: false,
            appBar: AppBar(
              backgroundColor: Config.mainColor,
              brightness: Brightness.light,
              iconTheme: IconThemeData(color: Colors.white),
              // leading: BackButton(
              //   onPressed: () {
              //     Get.offAllNamed(RouteConfig.main);
              //   },
              //   color: Colors.white,
              // ),
              title: CupertinoSegmentedControl<int>(
                borderColor: Colors.white,
                unselectedColor: Config.mainColor,
                selectedColor: Colors.white,
                groupValue: state.tabselectIndex,
                onValueChanged: (value) {
                  print('value================>');
                  print(value);
                  state.controller = TextEditingController();
                  state.controller1 = RefreshController();
                  logic.onTabSelect(value);
                },
                children: {
                  1: Container(
                    child: Text(
                      '自购下单',
                      style: TextStyle(
                          fontSize: 24.sp,
                          color: state.tabselectIndex == 1
                              ? Config.mainColor
                              : Colors.white),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 5.w),
                  ),
                  2: Container(
                    child: Text(
                      '分享下单',
                      style: TextStyle(
                          fontSize: 24.sp,
                          color: state.tabselectIndex == 2
                              ? Config.mainColor
                              : Colors.white),
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 5.w),
                  ),
                },
              ),
              centerTitle: true,
              elevation: 0,
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.all(30.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                  child: Container(
                    height: 57.w,
                    decoration: BoxDecoration(
                      color: Color(0xfffF7F8FA),
                      borderRadius: BorderRadius.all(
                        Radius.circular(35.w),
                      ),
                    ),
                    child: Center(
                      child: TextField(
                        controller: state.controller,
                        textAlignVertical: TextAlignVertical.center,
                        decoration: InputDecoration(
                          isDense: true,
                          prefixIcon: Icon(
                            Icons.search_rounded,
                            size: 18,
                            color: Color(0xfffCCCCCC),
                          ),
                          contentPadding: EdgeInsets.all(0),
                          prefixIconConstraints: BoxConstraints(
                            maxWidth: 70.w,
                            minWidth: 70.w,
                          ),
                          hintText: '请输入姓名/手机号/订单号搜索',
                          hintStyle: TextStyle(
                            fontSize: 28.sp,
                            color: Color(0xffDDDDDD),
                          ),
                          border: InputBorder.none,
                        ),
                        onChanged: (value) {
                          logic.onSearch(value);
                        },
                      ),
                    ),
                  ),
                ),
                /// tab
                Container(
                  height: 90.w,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                      bottom: BorderSide(
                        color: Color(0xffeeeeee),
                        width: 1.w,
                      ),
                    ),
                  ),
                  child: TabBar(
                    onTap: (index) {
                      logic.onTabChange(index);
                    },
                    controller: state.tabController,
                    isScrollable: true,
                    indicatorColor: Config.mainColor,
                    indicatorWeight: 3.0,
                    indicator: UnderlineTabIndicator(
                      insets: EdgeInsets.only(left: 80.w, right: 80.w),
                      borderSide:
                          BorderSide(width: 6.w, color: Config.mainColor),
                    ),
                    unselectedLabelColor: Colors.black,
                    indicatorPadding: EdgeInsets.symmetric(horizontal: 30.w),
                    labelStyle: TextStyle(
                      fontSize: 28.sp,
                      color: Color(0xff666666),
                    ),
                    labelColor: Config.mainColor,
                    tabs: <Widget>[

                      Tab(text: "全部"),
                      Tab(text: "待付款"),
                      Tab(text: "待配送/自提"),
                      Tab(text: "待送达"),
                      Tab(text: "待评价"),
                      Tab(text: "售后"),
                    ],
                  ),
                ),

                Container(
                  padding:
                      EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
                  child: RichText(
                    text: TextSpan(
                      children: [
                        TextSpan(
                          text: '共',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 28.w,
                          ),
                        ),
                        TextSpan(
                          text: ' ' + state.totalCount.toString() + ' ',
                          style: TextStyle(
                            color: Colors.red,
                            fontSize: 32.w,
                          ),
                        ),
                        TextSpan(
                          text: '笔订单',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 28.w,
                          ),
                        ),
                        WidgetSpan(
                          child: SizedBox(
                            width: 10.w,
                          ),
                        ),
                        TextSpan(
                          text: '共获得商品佣金',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 28.w,
                          ),
                        ),
                        TextSpan(
                          text: " ${state.totalOrderMoney} ",
                          style: TextStyle(
                            color: Colors.red,
                            fontSize: 32.w,
                          ),
                        ),
                        TextSpan(
                          text: '元',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 28.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    // padding: EdgeInsets.symmetric(horizontal: 26.w),

                    child:state.orderList.length>0? SmartRefresher(header: CustomHeader(
                  builder: (BuildContext context, RefreshStatus mode) {
                    Widget body;
                    if (mode == RefreshStatus.completed) {
                      body = CupertinoActivityIndicator();
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text("没有更多数据了");
                    } else if (mode == LoadStatus.loading) {
                      body = CupertinoActivityIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = Text("刷新失败");
                    } else {
                      body = Text("没有更多数据了");
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                      controller: state.controller1,
                      onRefresh: logic.refreshData,
                      onLoading: logic.loadMoreData,
                      enablePullUp: true,
                      enablePullDown: true,
                      child: ListView.builder(
                        shrinkWrap: true,
                        itemBuilder: (ctx, i) {
                          var data = state.orderList[i];
                          return state.tabselectIndex == 1
                              ? ItemWidget1(
                                  data: data,
                                  logic: logic,
                                )

                              : ItemWidget2(
                                  data: data,
                                  status: logic.state.tabController.index,
                                );
                        },
                        itemCount: state.orderList.length,
                      ),
                    ):Center(
                      child: Column(
                        children: [
                          SizedBox(
                            height: 140.w,
                          ),
                          Image(
                            image: AssetImage(
                                "assets/images/nodata.png"),
                            width: 218.w,
                            height: 164.w,
                          ),
                          SizedBox(
                            height: 20.w,
                          ),
                          Text(
                            '暂无数据',
                            style: TextStyle(
                              color: Color(0xff999999),
                              fontSize: 28.w,
                            ),),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
